<?php
/**
 * Created by PhpStorm.
 * User: g
 * Date: 2018/1/18
 * Time: 17:55
 */

namespace app\index\controller;
use think\Db;
use think\Request;

class Commonti extends Common
{
    public $typearr=[
                        5=>["name"=>"出差申请","table"=>"chuchai_apply"],
                        6=>["name"=>"万用申请","table"=>"wanneng_apply"],
                        7=>["name"=>"经销商合同申请","table"=>"jxs_hetong"],
                        8=>["name"=>"预算及目标申请","table"=>"target_apply"],
                        9=>["name"=>"预算及目标异动申请","table"=>"target_apply"],
                        10=>["name"=>"日常费用报销申请","table"=>"rc_baoxiao_apply"],
                        11=>["name"=>"AP费用申请","table"=>"ap_apply"],
                        12=>["name"=>"AP费用取消","table"=>"ap_apply"],
                        13=>["name"=>"AP费用异动","table"=>"ap_apply"],
                        14=>["name"=>"AP费用反馈","table"=>"ap_fankui"],
                        15=>["name"=>"AP费用核销","table"=>"ap_hexiao_apply"],
                        16=>["name"=>"AP补发票申请","table"=>"apti_fp_apply"],
                        17=>["name"=>"TI费用反馈","table"=>"all_ti_feeback_list"],
                        18=>["name"=>"TI费用核销","table"=>"cg_ti_hexiao"],
                        19=>["name"=>"TI补发票申请","table"=>"apti_fp_apply"],
                        20=>["name"=>"渠道优惠政策申请","table"=>"cg_channel_preferential_policy"],
                        21=>["name"=>"TI费用异动申请","table"=>"ti_apply"],
                        22=>["name"=>"TI费用取消申请","table"=>"ti_apply"],
                        23=>["name"=>"订单申请","table"=>"cg_order"],
                        24=>["name"=>"TPM专项费用申请","table"=>"ti_apply"]
                    ];
    //列表页按钮提交工作流
    public function gowork($fid,$aid){
        $flowId = db('cg_workflowset')->where(['id'=>$fid])->value('flow_id');
        $data = [];
        $data['accessToken'] = session('accessToken');
        $data['flowId'] = $flowId;
        $data = $this->curl_post(config('workflow_url').'/horizon/workflow/rest/flow/support/open.wf',$data);
        if($data->flowMsg=='提交成功'){
            $insert = [];
            
            if($fid==14){
                $row = db($this->typearr[$fid]["table"])->where('id',$aid)->field('no')->find();
                $insert['rwmcjbh'] ='AP费用反馈'.'['.$row['no'].']';
            }else if($fid==15){
                $row = db($this->typearr[$fid]["table"])->where('id',$aid)->field('no,apply_id')->find();
                $title=db("ap_apply")->where(["id"=>$row["apply_id"]])->value("title");
                $insert['rwmcjbh'] = $title."核销申请".'['.$row['no'].']';
            }else if($fid==16){
                $row = db($this->typearr[$fid]["table"])->where('id',$aid)->field('no,gid')->find();
                $title=db("gys")->where(["id"=>$row['gid']])->value("gys_name");
                $insert['rwmcjbh'] = "AP补发票申请".$title.'['.$row['no'].']';
            }else if($fid==17){
                $row = db($this->typearr[$fid]["table"])->where('id',$aid)->field('no,apply_id')->find();
                $title=db("cg_ti")->where(["id"=>$row['apply_id']])->value("title");
                $insert['rwmcjbh'] = $title.'['.$row['no'].']';
            }else if($fid==18){
                $row = db($this->typearr[$fid]["table"])->where('id',$aid)->field('sn as no,jxs_id')->find();
                $title=db("cg_jxs")->where(["id"=>$row['jxs_id']])->value("title");
                $insert['rwmcjbh'] = $title.'核销申请['.$row['no'].']';
            }else{
                $row = db($this->typearr[$fid]["table"])->where('id',$aid)->field('title,no')->find();
                $insert['rwmcjbh'] = $row['title'].'['.$row['no'].']';
            }
            
            $insert['yw_id'] = $aid;
            $insert['ywlx'] = $fid;
            $insert['status'] = 1;
            $insert['department_id'] = db('department_employee')->where(['index'=>1,'employee_id'=>session('employee_id')])->value('department_id');
            $insert['sqr_id'] = session('employee_id');
            $insert['workId'] = $data->workId;
            $insert['trackId'] = $data->flowNode->trackId;
            db('cg_workflow')->insert($insert);
        }else{
            return json_encode(['state'=>2,'msg'=>$data->flowMsg]);die;
        }

         if($fid==18){
            $bool = db($this->typearr[$fid]["table"])->where('id',$aid)->update(['status'=>3]);
        }else
            $bool = db($this->typearr[$fid]["table"])->where('id',$aid)->update(['state'=>3]);
        if($fid==9){
            //异动则锁定原单
            $yuanid= db($this->typearr[$fid]["table"])->where(['id'=>$aid])->value("yuanid");
            db($this->typearr[$fid]["table"])->where(['id'=>$yuanid])->update(["islock"=>1]);
        }
        if($bool){
            return json_encode(['state'=>1,'msg'=>'提交成功']);
        }else{
            return json_encode(['state'=>2,'msg'=>'系统繁忙请稍后再试']);
        }
    }
     //自动提交
    public function goworker($fid,$aid,$nodeKey,$selectId,$selectName){
        $flow = db('cg_workflow')->where(["ywlx"=>$fid,"yw_id"=>$aid])->field("trackId,workId")->find();
        $workId=$flow["workId"];$trackId=$flow["trackId"];
      
         $actionData = [
                    'operate'=>'submit',
                    'operateClazz'=>'com.horizon.wf.action.ActionSubmit',
                    'operateText'=>'提 交',
                    'operateFlag'=>0,
                    'nextNodes'=>[
                        [
                            'nodeKey'=>$nodeKey,
                            'selectId'=>$selectId,
                            'selectName'=>$selectName,
                            'isSelect'=>"true",
                            
                        ]
                    ],
                    "subFlows"=>null
                ];
                $param = [
                    'accessToken'=>session("accessToken"),
                    'workId'=>$workId,
                    'trackId'=>$trackId,
                    'actionData'=>$actionData,
                    "comment"=>"",
                    "dataId"=>"",
                ];
        $data = $this->curl_post(config('workflow_url').'/horizon/workflow/rest/flow/support/action.wf',$param);

        $data = object_array($data);
        return $data;
    }
     //获取按钮
    public function getbton($fid,$aid){


        $flow = db('cg_workflow')->where(["ywlx"=>$fid,"yw_id"=>$aid])->field("trackId,workId")->find();
        $workId=$flow["workId"];$trackId=$flow["trackId"];$nodeId="End1";

         //打开工作流
        $post_data = [];
        $post_data['accessToken'] = session('accessToken');
        $post_data['workId'] = $workId;
        $post_data['trackId'] = $trackId;
        $dataopen = $this->curl_post(config('workflow_url').'/horizon/workflow/rest/flow/support/open.wf',$post_data);


        $actionData = [
                    'operate'=>'submit',
                    'operateClazz'=>'com.horizon.wf.action.ActionSubmit',
                    'operateText'=>'提 交',
                    'operateFlag'=>1,
                ];
                $param = [
                    'accessToken'=>session("accessToken"),
                    'workId'=>$workId,
                    'trackId'=>$trackId,
                    
                    'actionData'=>$actionData,
                ];
        $data = $this->curl_post(config('workflow_url').'/horizon/workflow/rest/flow/support/action.wf',$param);
        $data = object_array($data);
       // print_r($data["nextNodes"]);
        return $data;
    }
    //获取审批信息
    public function getlog($aid){
        $type=db("cg_ti")->where("id=$aid")->value("type_id");
        if($type==0){
            $fid=2;
        }else if($type==1){
            $fid=1;
        }else if($type==2){
            $fid=3;
        }else if($type==3){ //后来添加的
            //$fid=4;
            $fid=21;
        }

        $cate_id=db("cg_ti")->where("id=$aid")->value("cate_id");
        if($type<=2){
            if($cate_id==2)
                $fid=21;
            else if($cate_id==3)
                $fid=22;
        }
        
        $result=[];
        $getwork=db('cg_workflow')->where(["ywlx"=>$fid,"yw_id"=>$aid])->value("workid");
        if($getwork){
            $result=Db::connect('db_config1')->name('tw_hz_log') 
            ->where(['workid'=>$getwork,"action"=>"submit"])
            ->select();
        }

        //print_r($result);
        return $result;
    }
    public function jxsyj($aid){
        $apply=Db::Table("cg_ti")->where(["id"=>$aid])->find();
        $jxs=Db::table("cg_jxs")->where(["id"=>$apply["jxs_id"]])->find();
        $this->assign("jxs",$jxs);
        //当年
        $now=$apply["ctime"];
        $cid=Db::table("cg_cainian")->where("starttime<=$now and endtime>=$now")->value("id");
        //当年考核期
        $kao2=Db::table("cg_kaoheqi1")->alias("k1")->join("cg_kaoheqi2 k2","k2.pid=k1.id")->where(["k1.cid"=>$cid])->order("k2.starttime")
        ->field("k1.id as k1id,k1.title as k1title,k1.starttime as k1start,k1.endtime as k1end,k2.id as k2id,k2.title as k2title,k2.starttime as k2start,k2.endtime as k2end")->select();
        $zong["tr"]=0;$zong["fy"]=0;$zong["ys"]=0;$zong["tour"]=0;$zong["mbfylh"]=0;$zong["mbfylx"]=0;$zong["sjfylh"]=0;$zong["sjfylx"]=0;
        $ys=0;
        for($i=0;$i<count($kao2);$i++){
            $ys++;
            //计算天数
            $k1ts=($kao2[$i]["k1end"]-$kao2[$i]["k1start"])/3600/24+1;
            $k2ts=($kao2[$i]["k2end"]-$kao2[$i]["k2start"])/3600/24+1;
            //当年目标
            $kao2[$i]["tar"]=0;
            $sumk2tar=Db::table("target_apply")->alias("t")->join("target_apply_detail td","td.apply_id=t.id")
            ->where("t.type_id=5 and t.state=7 and t.tag<>1 and t.is_actived=1 and t.is_deleted=0 and td.jingxiao={$jxs["id"]} and td.erjikaoheqi={$kao2[$i]["k2id"]}")->sum("feiyong");
            if($sumk2tar)
                $kao2[$i]["tar"]+=$sumk2tar;
            $sumk1tar=Db::table("target_apply")->alias("t")->join("target_apply_detail td","td.apply_id=t.id")
            ->where("t.type_id=5 and t.state=7 and t.tag<>1 and t.is_actived=1 and t.is_deleted=0 and td.jingxiao={$jxs["id"]} and td.yijikaoheqi={$kao2[$i]["k1id"]} and td.erjikaoheqi is null")->sum("feiyong");
            //echo Db::table("target_apply")->getLastsql();
            if($sumk1tar){
                $kao2[$i]["tar"]+=$sumk1tar/$k1ts*$k2ts;
            }
            $zong["tr"]+=$kao2[$i]["tar"];
            //发运
            $datast=date("Y-m-d",$kao2[$i]["k2start"]);
            $dataed=date("Y-m-d",$kao2[$i]["k2end"]);
            $sumfy=Db::Table("bx_fy")->where("sdAgency_id={$jxs["id"]} and fyDate>='$datast' and fyDate<='$dataed'")->sum("fyZheMoney");
            //echo Db::table("bx_fy")->getLastsql();
            if($sumfy){
                $kao2[$i]["fy"]=$sumfy;
            }else{
                $kao2[$i]["fy"]=0;
            }
            $zong["fy"]+=$kao2[$i]["fy"];
            //预算费用
            $kao2[$i]["ys"]=0;
            $sumk2ys=Db::Table("yusuan")->alias("y")->where("y.type=1 and y.jingxiao={$jxs["id"]} and erjikaoheqi={$kao2[$i]["k2id"]}")->sum("jine");
            if($sumk2ys){
                $kao2[$i]["ys"]+=$sumk2ys;
            }
            $sumk1ys=Db::Table("yusuan")->alias("y")->where("y.type=1 and y.jingxiao={$jxs["id"]} and yijikaoheqi={$kao2[$i]["k1id"]} and erjikaoheqi is null")->sum("jine");
            if($sumk1ys){
                $kao2[$i]["ys"]+=$sumk1ys/$k1ts*$k2ts;
            }
            $zong["ys"]+=$kao2[$i]["ys"];
            //投入额
            $kao2[$i]["tour"]=0;
            $sumks=Db::table("cg_ti")->alias("t")
            ->join("cg_ti_list td","t.id=td.ti_id")
            ->where("t.status in (7,9) and t.status2 is null and (td.isOrNo is null or td.isOrNo=1) and td.start>={$kao2[$i]["k2start"]} and td.start<={$kao2[$i]["k2end"]} and t.jxs_id={$jxs["id"]}")->field("pizhunhanshui,totalExpectedSales,fb_shijitourufeiyong,fb_shijixiaoliang")->select();
            $kao2[$i]["mbfylh"]=0;$kao2[$i]["mbfylx"]=0;$kao2[$i]["sjfylh"]=0;$kao2[$i]["sjfylx"]=0;
            for($j=0;$j<count($sumks);$j++){
                if($sumks[$j]["fb_shijitourufeiyong"]){
                    $kao2[$i]["tour"]+=$sumks[$j]["fb_shijitourufeiyong"];
                    $kao2[$i]["sjfylh"]+=$sumks[$j]["fb_shijitourufeiyong"];
                    $kao2[$i]["sjfylx"]+=$sumks[$j]["fb_shijixiaoliang"];
                }else{
                    $kao2[$i]["tour"]+=$sumks[$j]["pizhunhanshui"];
                }
                $kao2[$i]["mbfylh"]+=$sumks[$j]["pizhunhanshui"];
                $kao2[$i]["mbfylx"]+=$sumks[$j]["totalExpectedSales"];
            }
            if($kao2[$i]["tar"]&&$kao2[$i]["fy"])
                $kao2[$i]["mbfyl"]=$kao2[$i]["fy"]/$kao2[$i]["tar"]*100;
            else
                $kao2[$i]["mbfyl"]=0;
            if($kao2[$i]["sjfylx"])
                $kao2[$i]["sjfyl"]=$kao2[$i]["sjfylh"]/$kao2[$i]["sjfylx"]*100;
            else
                $kao2[$i]["sjfyl"]=0;
            $zong["tour"]+=$kao2[$i]["tour"];
            $zong["mbfylh"]+=$kao2[$i]["mbfylh"];
            $zong["mbfylx"]+=$kao2[$i]["mbfylx"];
            $zong["sjfylh"]+=$kao2[$i]["sjfylh"];
            $zong["sjfylx"]+=$kao2[$i]["sjfylx"];
        }
        $zong["avgtr"]=$zong["tr"]/$ys;
        $zong["avgfy"]=$zong["fy"]/$ys;
        $zong["avgys"]=$zong["ys"]/$ys;
        $zong["avgtour"]=$zong["tour"]/$ys;
        if($zong["tr"]&&$zong["ys"])
            $zong["mbfyl"]=$zong["ys"]/$zong["tr"]*100;
        else
            $zong["mbfyl"]=0;
        if($zong["sjfylx"])
            $zong["sjfyl"]=$zong["sjfylh"]/$zong["sjfylx"]*100;
        else
            $zong["sjfyl"]=0;
        $this->assign("zong",$zong);
        $this->assign("kao2",$kao2);
        //决策依据2
        $startday=strtotime("2017-01-01");$t=time();
        $cainian=Db::table("cg_cainian")->where("starttime>=$startday and starttime<=$t")->select();
        $zongcai=count($cainian);
        $hasc=[];
        for($i=0;$i<count($cainian);$i++){
            $sum=Db::table("cg_ti")->alias("t")
            ->join("cg_ti_list td","t.id=td.ti_id")
            ->where("t.status in (7,9) and t.status2 is null and (td.isOrNo is null or td.isOrNo=1) and td.start>={$cainian[$i]["starttime"]} and td.start<={$cainian[$i]["endtime"]} and t.jxs_id={$jxs["id"]}")->sum("pizhunhanshui");
            
            if($sum)
                $hasc[]=$cainian[$i];
        }
        for($i=0;$i<count($hasc);$i++){
            $hasc[$i]["kaohe2"]=Db::table("cg_cainian")->alias("c")->join("cg_kaoheqi1 k1","k1.cid=c.id")->join("cg_kaoheqi2 k2","k2.pid=k1.id")->where(["c.id"=>$hasc[$i]["id"]])
            ->field("k2.id,k2.title,k2.starttime,k2.endtime")->order("k2.starttime")->select();
            //查询单据类型
            $hasc[$i]["feetype"]=Db::table("cg_ti")->alias("t")
            ->join("cg_ti_list td","t.id=td.ti_id")
            ->join("cg_feiyongkemu3 k3","k3.id=td.feetype")
            ->where("t.status in (7,9) and t.status2 is null and (td.isOrNo is null or td.isOrNo=1) and td.start>={$hasc[$i]["starttime"]} and td.start<={$hasc[$i]["endtime"]} and t.jxs_id={$jxs["id"]}")
            ->group("td.feetype")
            ->field("k3.title as feetype,k3.id")->select();
            //echo db("cg_ti")->getLastsql();
            $hasc[$i]["feec"]=count($hasc[$i]["feetype"]);
            $hasc[$i]["xiaojizong"]=0;
            for($k=0;$k<count($hasc[$i]["kaohe2"]);$k++){
                $hasc[$i]["xiaoji"][$k]=0;
                $tmp=Db::table("cg_ti")->alias("t")
                    ->join("cg_ti_list td","t.id=td.ti_id")->where("t.status in (7,9) and t.status2 is null and (td.isOrNo is null or td.isOrNo=1) and td.start>={$hasc[$i]["kaohe2"][$k]["starttime"]} and td.start<={$hasc[$i]["kaohe2"][$k]["endtime"]} and t.jxs_id={$jxs["id"]}")
                    ->field("td.pizhunhanshui,td.fb_shijitourufeiyong")->select();
                for($l=0;$l<count($tmp);$l++){
                    if($tmp[$l]["fb_shijitourufeiyong"])
                        $hasc[$i]["xiaoji"][$k]+=$tmp[$l]["fb_shijitourufeiyong"];
                    else
                        $hasc[$i]["xiaoji"][$k]+=$tmp[$l]["pizhunhanshui"];

                }
                $hasc[$i]["xiaojizong"]+=$hasc[$i]["xiaoji"][$k];
            }
            for($j=0;$j<count($hasc[$i]["feetype"]);$j++){
                $hasc[$i]["feetype"][$j]["zong"]=0;
                for($k=0;$k<count($hasc[$i]["kaohe2"]);$k++){
                    $tmp=Db::table("cg_ti")->alias("t")
                    ->join("cg_ti_list td","t.id=td.ti_id")->where("t.status in (7,9) and t.status2 is null and (td.isOrNo is null or td.isOrNo=1) and td.start>={$hasc[$i]["kaohe2"][$k]["starttime"]} and td.start<={$hasc[$i]["kaohe2"][$k]["endtime"]} and t.jxs_id={$jxs["id"]} and td.feetype={$hasc[$i]["feetype"][$j]["id"]}")
                    ->field("td.pizhunhanshui,td.fb_shijitourufeiyong")->select();
                    $hasc[$i]["feetype"][$j]["fee"][$k]=0;
                    for($l=0;$l<count($tmp);$l++){
                        if($tmp[$l]["fb_shijitourufeiyong"])
                            $hasc[$i]["feetype"][$j]["fee"][$k]+=$tmp[$l]["fb_shijitourufeiyong"];
                        else
                            $hasc[$i]["feetype"][$j]["fee"][$k]+=$tmp[$l]["pizhunhanshui"];

                    }
                    $hasc[$i]["feetype"][$j]["zong"]+=$hasc[$i]["feetype"][$j]["fee"][$k];
                }
            }
        }

        $this->assign("hasc",$hasc);
        //决策一举三
        $nownian=Db::table("cg_cainian")->where("id=$cid")->find();
        $nowfee=Db::table("cg_ti")->alias("t")
            ->join("cg_ti_list td","t.id=td.ti_id")
            ->join("cg_feiyongkemu3 k3","k3.id=td.feetype")
            ->where("t.status in (7,9) and t.status2 is null and (td.isOrNo is null or td.isOrNo=1) and td.start>={$nownian["starttime"]} and td.start<={$nownian["endtime"]} and t.jxs_id={$jxs["id"]}")
            ->group("td.feetype")
            ->field("k3.title as feetype,k3.id")->select();
        for($i=0;$i<count($nowfee);$i++){
            $tmp=Db::table("cg_ti")->alias("t")
                    ->join("cg_ti_list td","t.id=td.ti_id")->where("t.status in (7,9) and t.status2 is null and (td.isOrNo is null or td.isOrNo=1) and td.start>={$nownian["starttime"]} and td.start<={$nownian["endtime"]} and t.jxs_id={$jxs["id"]} and td.feetype={$nowfee[$i]["id"]}")
                    ->field("td.pizhunhanshui,td.fb_shijitourufeiyong")->select();
            $nowfee[$i]["fee"]=0;
            for($l=0;$l<count($tmp);$l++){
                if($tmp[$l]["fb_shijitourufeiyong"])
                    $nowfee[$i]["fee"]+=$tmp[$l]["fb_shijitourufeiyong"];
                else
                    $nowfee[$i]["fee"]+=$tmp[$l]["pizhunhanshui"];

            }
        }
        $this->assign("nowfee",$nowfee);
        $today=Date("y-m-d");
        $this->assign("today",$today);
        //决策依据四
        $fy=Db::Table("bx_fy")->alias("f")->join("pro_products p","f.product_id=p.id")->where("f.sdAgency_id={$jxs["id"]}")->order("fyDate desc")
        ->field("p.id as pid,p.pro_no,p.pro_name,f.fyDate,f.fyZheMoney")->limit(0,5)->select();
        $this->assign("fy",$fy);
        //决策依据6
        //$zongc=[];
        for($i=0;$i<$zongcai;$i++){
            $cainian[$i]["zongfy"]=0;
            //echo $i;
            $kaohe2=Db::table("cg_cainian")->alias("c")->join("cg_kaoheqi1 k1","k1.cid=c.id")->join("cg_kaoheqi2 k2","k2.pid=k1.id")->where(["c.id"=>$cainian[$i]["id"]])
            ->field("k2.id,k2.title,k2.starttime as k2start,k2.endtime as k2end")->order("k2.starttime")->select();
            //print_r($kaohe2);echo "<br/>";
            $zongc["zongfy"]=0;
            for($j=0;$j<count($kaohe2);$j++){
                $datast=date("Y-m-d",$kaohe2[$j]["k2start"]);
                $dataed=date("Y-m-d",$kaohe2[$j]["k2end"]);
                $sumfy=Db::Table("bx_fy")->where("sdAgency_id={$jxs["id"]} and fyDate>='$datast' and fyDate<='$dataed'")->sum("fyZheMoney");
                if($sumfy)
                    $cainian[$i]["fy"][$j]=$sumfy;
                else
                    $cainian[$i]["fy"][$j]=0;
                $cainian[$i]["zongfy"]+=$cainian[$i]["fy"][$j];
            }
            $cainian[$i]["avgfy"]=$cainian[$i]["zongfy"]/12;
        }
        //print_r($cainian);
       // $this->assign("zongc",$zongc);
        $this->assign("cainian",$cainian);
        return $this->fetch();
    }
    //网点情况
    public function netyj($did){
        $de=Db::table("cg_ti_list")->where(["id"=>$did])->find();
        $net=Db::table("cg_net")->where(["id"=>$de["net_id"]])->find();
        $this->assign("net",$net);
        //条码情况
        $tm=Db::Table("cg_net_barcode")->alias("b")->join("pro_products p","b.prod=p.id")->where(["b.net_id"=>$net["id"],"tuichang"=>0])
        ->field("p.pro_no,p.pro_name,b.intime,b.infee,b.retailPrice")->select();
        for($i=0;$i<count($tm);$i++){
            $tm[$i]["intime"]=date("Y-m-d",$tm[$i]["intime"]);
        }
        $this->assign("tm",$tm);
        $ctm=count($tm);
        $this->assign("ctm",$ctm);
        //退场条码
        $tuim=Db::Table("cg_net_barcode")->alias("b")->join("pro_products p","b.prod=p.id")->where(["b.net_id"=>$net["id"],"tuichang"=>1])
        ->field("p.pro_no,p.pro_name,b.tuichangshijian,b.infee,b.retailPrice,b.tuichangyuanyin")->select();
        for($i=0;$i<count($tuim);$i++){
            $tuim[$i]["tuichangshijian"]=date("Y-m-d",$tuim[$i]["tuichangshijian"]);
        }
        $this->assign("tuim",$tuim);
        $ctuim=count($tuim);
        $this->assign("ctuim",$ctuim);
        //竞品情况
        $jp=Db::table("cg_net_competitor")->alias("n")->join("competitor c","n.name=c.id")->join("competitor_category cc","c.category_id=cc.id")
        ->where(["n.isdel"=>0,"n.net_id"=>$net["id"],"status"=>0])
        ->field("cc.category_name,c.c_name,c.format,n.price,n.brief")->select();
        $this->assign("jp",$jp);
        $cjp=count($jp);
        $this->assign("cjp",$cjp);
        //导购情况
        $dg=Db::table("cg_net_dg")->alias("d")->join("employee e","d.dg_id=e.id")->where("d.net_id={$net["id"]}")
        ->field("e.em_name,e.gender,e.mobile,e.state,e.entry")->select();
        $this->assign("dg",$dg);
        $cdg=count($dg);
        $this->assign("cdg",$cdg);
        //最近5次活动
        $hd=Db::table("cg_ti")->alias("t")
        ->join("cg_ti_list td","t.id=td.ti_id")
        ->where("t.status in (7,9) and t.status2 is null and (td.isOrNo is null or td.isOrNo=1) and td.net_id={$net["id"]}")
        ->order("td.start desc")->limit(0,5)
        ->field("t.sn,td.sn as tdsn,t.type_id,t.cuser,td.feetype,td.pizhunhanshui,td.fb_shijitourufeiyong,td.totalExpectedSales,td.fb_shijixiaoliang,td.start,td.end,td.product")
        ->select();
        for($i=0;$i<count($hd);$i++){
            $hd[$i]["cuser"]=Db::Table("employee")->where(["id"=>$hd[$i]["cuser"]])->value("em_name");
            if($hd[$i]["product"])
                $hd[$i]["product"]=Db::table("pro_products")->where(["id"=>$hd[$i]["product"]])->value("pro_name");
            $hd[$i]["start"]=date("Y-m-d",$hd[$i]["start"]);
            $hd[$i]["end"]=date("Y-m-d",$hd[$i]["end"]);
            $hd[$i]["feetype"]=Db::table("cg_feiyongkemu3")->where(["id"=>$hd[$i]["feetype"]])->value("title");
        }
        $this->assign("hd",$hd);
        $chd=Db::table("cg_ti")->alias("t")
        ->join("cg_ti_list td","t.id=td.ti_id")
        ->where("t.status in (7,9) and t.status2 is null and (td.isOrNo is null or td.isOrNo=1)")->count();
        if(!$chd)
            $chd=0;
        $this->assign("chd",$chd);
        //网点拜访情况
        $bf=Db::table("yd_wdbf")->alias("bf")->join("employee e","bf.bfr=e.id")
        ->join("department_employee de","de.employee_id=e.id")->join("department d","d.id=de.department_id")
        ->where(["bf.wdid"=>$net["id"],"de.index"=>1])->order("dzsj desc")
        ->field("e.em_name,d.de_name,bf.ddsj,bf.dzsj,bf.rq")->select();
        $this->assign("bf",$bf);
        $cbf=count($bf);
        $this->assign("cbf",$cbf);
        //决策依据1
        $startday=strtotime("2017-01-01");
        $cainian=Db::table("cg_cainian")->where("starttime>=$startday")->select();
        $hasc=[];
        for($i=0;$i<count($cainian);$i++){
            $sum=Db::table("cg_ti")->alias("t")
            ->join("cg_ti_list td","t.id=td.ti_id")
            ->where("t.status in (7,9) and t.status2 is null and (td.isOrNo is null or td.isOrNo=1) and td.start>={$cainian[$i]["starttime"]} and td.start<={$cainian[$i]["endtime"]} and td.net_id={$net["id"]}")->sum("yjsjtrfy");
            $st=date("Y-m-d",$cainian[$i]["starttime"]);
            $ed=date("Y-m-d",$cainian[$i]["endtime"]);
            $sumfy=Db::table("bx_fy2")->where("wd_id={$net["id"]} and fy2Date<='$ed' and fy2Date>='$st'")->sum("money");
            if($sum||$sumfy)
                $hasc[]=$cainian[$i];
        }
        for($i=0;$i<count($hasc);$i++){
            $hasc[$i]["kaohe2"]=Db::table("cg_cainian")->alias("c")->join("cg_kaoheqi1 k1","k1.cid=c.id")->join("cg_kaoheqi2 k2","k2.pid=k1.id")->where(["c.id"=>$hasc[$i]["id"]])
            ->field("k2.id,k2.title,k2.starttime,k2.endtime")->order("k2.starttime")->select();
            $hasc[$i]["zongzc"]=0;
            $hasc[$i]["zongfy"]=0;
            $hasc[$i]["zongxiaoliang"]=0;
            for($j=0;$j<count($hasc[$i]["kaohe2"]);$j++){
                $hasc[$i]["kaohe2"][$j]["zc"]='';
                $sumks=Db::table("cg_ti")->alias("t")
                ->join("cg_ti_list td","t.id=td.ti_id")
                ->where("t.status in (7,9) and t.status2 is null and (td.isOrNo is null or td.isOrNo=1) and td.start>={$hasc[$i]["kaohe2"][$j]["starttime"]} and td.start<={$hasc[$i]["kaohe2"][$j]["endtime"]} and td.net_id={$net["id"]}")->field("pizhunhanshui,fb_shijitourufeiyong,fb_shijixiaoliang")->select();
                //print_r($sumks);echo "<br>";
                $sumk=0;$xiaoliang=0;
                for($k=0;$k<count($sumks);$k++){
                    if($sumks[$k]["fb_shijitourufeiyong"])
                        $sumk+=$sumks[$k]["fb_shijitourufeiyong"];
                    else
                        $sumk+=$sumks[$k]["pizhunhanshui"];
                    if($sumks[$k]["fb_shijixiaoliang"]){
                        $xiaoliang+=$sumks[$k]["fb_shijixiaoliang"];
                    }

                }

                if($sumk){
                    $hasc[$i]["kaohe2"][$j]["zc"]=$sumk;
                    $hasc[$i]["zongzc"]+=$sumk;
                }
                if($xiaoliang){
                    $hasc[$i]["zongxiaoliang"]+=$xiaoliang;
                    $hasc[$i]["kaohe2"][$j]["xiaoliang"]=$xiaoliang;
                    $hasc[$i]["kaohe2"][$j]["fl"]=round($hasc[$i]["kaohe2"][$j]["zc"]/$hasc[$i]["kaohe2"][$j]["xiaoliang"]*100,2)."%";
                }else{
                    $hasc[$i]["kaohe2"][$j]["fl"]="-";
                    $hasc[$i]["kaohe2"][$j]["xiaoliang"]='-';
                }
                $hasc[$i]["kaohe2"][$j]["fy"]='';
                $st2=date("Y-m-d",$hasc[$i]["kaohe2"][$j]["starttime"]);
                $ed2=date("Y-m-d",$hasc[$i]["kaohe2"][$j]["endtime"]);
                $sumy=Db::table("bx_fy2")->where("wd_id={$net["id"]} and fy2Date<='$ed2' and fy2Date>='$st2'")->sum("money");
                if($sumy){
                    $hasc[$i]["kaohe2"][$j]["fy"]=$sumy;
                    $hasc[$i]["zongfy"]+=$sumy;
                }
            }
            $hasc[$i]["avgzc"]=$hasc[$i]["zongzc"]/12;
            $hasc[$i]["avgfy"]=$hasc[$i]["zongfy"]/12;
            if($hasc[$i]["zongxiaoliang"]){
                $hasc[$i]["fl"]=round($hasc[$i]["zongzc"]/$hasc[$i]["zongxiaoliang"]*100,2)."%";
            }else{
                $hasc[$i]["fl"]='-';
            }
        }
        $this->assign("hasc",$hasc);
        return $this->fetch();
    }
    //产品系列roi决策
    public function seriosroi($did){
        $de=Db::table("cg_ti_list")->where(["id"=>$did])->find();
        //$pro=Db::table("pro_products")->where(["id"=>$de["product"]])->find();
        $type_id=Db::table("cg_ti")->where(["id"=>$de["ti_id"]])->value("type_id");
        if($type_id==1){
            $de["product"]=$de["f1"];
            $year=$de["year"];
            $month=$de["month"];
            $de["start"]=strtotime("{$year}-{$month}-15");
            $de["end"]=strtotime("{$year}-{$month}-15");
        }
        if($de["product"]){
            $de["sku"]=Db::table("pro_products")->where(["id"=>$de["product"]])->value("sku_id");
        }
        if($de["sku"]){
            $de["series"]=Db::table("pro_sku")->where(["id"=>$de["sku"]])->value("series_id");
        }
        $series=Db::table("pro_series")->where(["id"=>$de["series"]])->find();
        $this->assign("series",$series);
        $profit=Db::Table("pro_brands")->alias("b")
        ->join("pro_category c","c.brand_id=b.id")
        ->join("pro_series s","s.category_id=c.id")->where(["s.id"=>$de["series"]])->value("b.profit");
        $net=Db::table("cg_net")->where(["id"=>$de["net_id"]])->find();

        $nian=Db::table("cg_cainian")->where("starttime<={$de["start"]} and endtime>={$de["end"]}")->find();
        $this->assign("net",$net);
        //$this->assign("pro",$pro);
        $pro=Db::Table("pro_products")->alias("p")->join("pro_sku sk","p.sku_id=sk.id")->join("pro_series s","sk.series_id=s.id")->where(["s.id"=>$de["series"]])->field("p.id")->select();
        $pid=[];
        for($i=0;$i<count($pro);$i++){
            $pid[]=$pro[$i]["id"];
        }
        if($pid){
            $pid=implode(',',$pid);
        }else
        {
            $pid=-1;
        }
        $this->assign("nian",$nian);
        $zongtianshu=($de["end"]-$de["start"])/24/3600+1;
        if($de["fb_shijitourufeiyong"])
            $hanshuis=$de["fb_shijitourufeiyong"];
        else
            $hanshuis=$de["pizhunhanshui"];

        $yujixiaoliang=$de["totalExpectedSales"];
        if($de["fb_shijixiaoliang"])
            $fankui=$de["fb_shijixiaoliang"];
        else
            $fankui=0;
        $kao1=Db::table("cg_kaoheqi1")->where(["cid"=>$nian["id"]])->order("starttime")->select();
       
        for($i=0;$i<count($kao1);$i++){
            $kao1[$i]["ts"]=($kao1[$i]["endtime"]-$kao1[$i]["starttime"])/24/3600+1;
            if($kao1[$i]["endtime"]>=$de["start"]&&$kao1[$i]["starttime"]<=$de["end"]){
                $st=$de["start"]>=$kao1[$i]["starttime"]?$de["start"]:$kao1[$i]["starttime"];
                $ed=$de["end"]<=$kao1[$i]["endtime"]?$de["end"]:$kao1[$i]["endtime"];
                $k1ts=($ed-$st)/3600/24+1;
            }else{
                $k1ts=0;
            }
            $kao1[$i]["kao2"]=Db::table("cg_kaoheqi2")->where(["pid"=>$kao1[$i]["id"]])->count();
            $kao1[$i]["kao2"]=$kao1[$i]["kao2"]+1;
            $kao1[$i]["k2"]=Db::table("cg_kaoheqi2")->where(["pid"=>$kao1[$i]["id"]])->order("starttime")->select();
            $kao1[$i]["base"]=0;
            $kao1[$i]["hanshuis"]=round($hanshuis/$zongtianshu*$k1ts,2);
            $kao1[$i]["fankui"]=round($fankui/$zongtianshu*$k1ts,2);

            for($j=0;$j<count($kao1[$i]["k2"]);$j++){
                $kao1[$i]["k2"][$j]["ts"]=($kao1[$i]["k2"][$j]["endtime"]-$kao1[$i]["k2"][$j]["starttime"])/24/3600+1;
                if($kao1[$i]["k2"][$j]["endtime"]>=$de["start"]&&$kao1[$i]["k2"][$j]["starttime"]<=$de["end"]){
                    $st=$de["start"]>=$kao1[$i]["k2"][$j]["starttime"]?$de["start"]:$kao1[$i]["k2"][$j]["starttime"];
                    $ed=$de["end"]<=$kao1[$i]["k2"][$j]["endtime"]?$de["end"]:$kao1[$i]["k2"][$j]["endtime"];
                    $k2ts=($ed-$st)/3600/24+1;
                }else{
                    $k2ts=0;
                }
                $yue=(int)$kao1[$i]["k2"][$j]["title"];
                //基准销量
                $base=Db::Table("cg_net_basic")->where("net_id={$net["id"]} and kaoheqi2=$yue and pro_id in ($pid)")->sum("num");

                //申请
                if(round($hanshuis/$zongtianshu*$k2ts,2))
                    $kao1[$i]["k2"][$j]["hanshuis"]=round($hanshuis/$zongtianshu*$k2ts,2);
                else{
                    $kao1[$i]["k2"][$j]["hanshuis"]='-';
                }
                if($base){
                     $kao1[$i]["k2"][$j]["base"]=$base;
                     $kao1[$i]["base"]+=$base;
                }else{
                     $kao1[$i]["k2"][$j]["base"]='-';
                     $base=0;
                }
                  //反馈销量
                if(round($fankui/$zongtianshu*$k2ts,2)){
                    $kao1[$i]["k2"][$j]["fankui"]=round($fankui/$zongtianshu*$k2ts,2);
                    $kao1[$i]["k2"][$j]["bi"]=round($kao1[$i]["k2"][$j]["hanshuis"]/$kao1[$i]["k2"][$j]["fankui"]*100,2)."%";
                    $kao1[$i]["k2"][$j]["roi"]=round((round($yujixiaoliang/$zongtianshu*$k2ts,2)-$base)*$profit/$kao1[$i]["k2"][$j]["hanshuis"]*100,2)."%";
                }
                else{
                    $kao1[$i]["k2"][$j]["fankui"]='-';
                    $kao1[$i]["k2"][$j]["bi"]="-";$kao1[$i]["k2"][$j]["roi"]="-";
                }
            }
            if($kao1[$i]["fankui"]){
                $kao1[$i]["bi"]=round($kao1[$i]["hanshuis"]/$kao1[$i]["fankui"]*100,2)."%";
                $kao1[$i]["roi"]=round((round($yujixiaoliang/$zongtianshu*$k1ts,2)-$kao1[$i]["base"])*$profit/$kao1[$i]["hanshuis"]*100,2)."%";
            }else{
                $kao1[$i]["bi"]="-";$kao1[$i]["roi"]='-';
            }
        }
        
        $this->assign("kao1",$kao1);
      
        
        
        return $this->fetch();
    }
    //产品roi决策 
    public function productroi($did){
        $de=Db::table("cg_ti_list")->where(["id"=>$did])->find();
         $type_id=Db::table("cg_ti")->where(["id"=>$de["ti_id"]])->value("type_id");
        if($type_id==1){
            $de["product"]=$de["f1"];
            $year=$de["year"];
            $month=$de["month"];
            $de["start"]=strtotime("{$year}-{$month}-15");
            $de["end"]=strtotime("{$year}-{$month}-15");
        }
        $pro=Db::table("pro_products")->where(["id"=>$de["product"]])->find();
        $profit=Db::Table("pro_brands")->alias("b")
        ->join("pro_category c","c.brand_id=b.id")
        ->join("pro_series s","s.category_id=c.id")
        ->join("pro_sku sk","sk.series_id=s.id")
        ->join("pro_products p","p.sku_id=sk.id")->where(["p.id"=>$de["product"]])->value("b.profit");
        $net=Db::table("cg_net")->where(["id"=>$de["net_id"]])->find();
        $nian=Db::table("cg_cainian")->where("starttime<={$de["start"]} and endtime>={$de["end"]}")->find();
        $this->assign("net",$net);
        $this->assign("pro",$pro);
        $this->assign("nian",$nian);
        $zongtianshu=($de["end"]-$de["start"])/24/3600+1;
        if($de["fb_shijitourufeiyong"])
            $hanshuis=$de["fb_shijitourufeiyong"];
        else
            $hanshuis=$de["pizhunhanshui"];

        $yujixiaoliang=$de["totalExpectedSales"];
        $jizhun=$de["totalBenchmarkSales"];
        if($de["fb_shijixiaoliang"])
            $fankui=$de["fb_shijixiaoliang"];
        else
            $fankui=0;
        $kao1=Db::table("cg_kaoheqi1")->where(["cid"=>$nian["id"]])->order("starttime")->select();
       
        for($i=0;$i<count($kao1);$i++){
            $kao1[$i]["ts"]=($kao1[$i]["endtime"]-$kao1[$i]["starttime"])/24/3600+1;
            if($kao1[$i]["endtime"]>=$de["start"]&&$kao1[$i]["starttime"]<=$de["end"]){
                $st=$de["start"]>=$kao1[$i]["starttime"]?$de["start"]:$kao1[$i]["starttime"];
                $ed=$de["end"]<=$kao1[$i]["endtime"]?$de["end"]:$kao1[$i]["endtime"];
                $k1ts=($ed-$st)/3600/24+1;
            }else{
                $k1ts=0;
            }
            $kao1[$i]["kao2"]=Db::table("cg_kaoheqi2")->where(["pid"=>$kao1[$i]["id"]])->count();
            $kao1[$i]["kao2"]=$kao1[$i]["kao2"]+1;
            $kao1[$i]["k2"]=Db::table("cg_kaoheqi2")->where(["pid"=>$kao1[$i]["id"]])->order("starttime")->select();
            $kao1[$i]["base"]=0;
            $kao1[$i]["hanshuis"]=round($hanshuis/$zongtianshu*$k1ts,2);
            $kao1[$i]["fankui"]=round($fankui/$zongtianshu*$k1ts,2);

            for($j=0;$j<count($kao1[$i]["k2"]);$j++){
                $kao1[$i]["k2"][$j]["ts"]=($kao1[$i]["k2"][$j]["endtime"]-$kao1[$i]["k2"][$j]["starttime"])/24/3600+1;
                if($kao1[$i]["k2"][$j]["endtime"]>=$de["start"]&&$kao1[$i]["k2"][$j]["starttime"]<=$de["end"]){
                    $st=$de["start"]>=$kao1[$i]["k2"][$j]["starttime"]?$de["start"]:$kao1[$i]["k2"][$j]["starttime"];
                    $ed=$de["end"]<=$kao1[$i]["k2"][$j]["endtime"]?$de["end"]:$kao1[$i]["k2"][$j]["endtime"];
                    $k2ts=($ed-$st)/3600/24+1;
                }else{
                    $k2ts=0;
                }
                $yue=(int)$kao1[$i]["k2"][$j]["title"];
                //基准销量
                $base=Db::Table("cg_net_basic")->where(["pro_id"=>$pro["id"],"net_id"=>$net["id"],"kaoheqi2"=>$yue])->value("num");

                //申请
                if(round($hanshuis/$zongtianshu*$k2ts,2))
                    $kao1[$i]["k2"][$j]["hanshuis"]=round($hanshuis/$zongtianshu*$k2ts,2);
                else{
                    $kao1[$i]["k2"][$j]["hanshuis"]='-';
                }
                if($base){
                     $kao1[$i]["k2"][$j]["base"]=$base;
                     $kao1[$i]["base"]+=$base;
                }else{
                     $kao1[$i]["k2"][$j]["base"]='-';
                     $base=0;
                }
                  //反馈销量
                if(round($fankui/$zongtianshu*$k2ts,2)){
                    $kao1[$i]["k2"][$j]["fankui"]=round($fankui/$zongtianshu*$k2ts,2);
                    $kao1[$i]["k2"][$j]["bi"]=round($kao1[$i]["k2"][$j]["hanshuis"]/$kao1[$i]["k2"][$j]["fankui"]*100,2)."%";
                    $kao1[$i]["k2"][$j]["roi"]=round((round($yujixiaoliang/$zongtianshu*$k2ts,2)-round($jizhun/$zongtianshu*$k2ts,2))*$profit/$kao1[$i]["k2"][$j]["hanshuis"]/100,2)."%";
                }
                else{
                    $kao1[$i]["k2"][$j]["fankui"]='-';
                    $kao1[$i]["k2"][$j]["bi"]="-";$kao1[$i]["k2"][$j]["roi"]="-";
                }
            }
            if($kao1[$i]["fankui"]){
                $kao1[$i]["bi"]=round($kao1[$i]["hanshuis"]/$kao1[$i]["fankui"]*100,2)."%";
                $kao1[$i]["roi"]=round((round($yujixiaoliang/$zongtianshu*$k1ts,2)-round($jizhun/$zongtianshu*$k1ts,2))*$profit/$kao1[$i]["hanshuis"]/100,2)."%";
            }else{
                $kao1[$i]["bi"]="-";$kao1[$i]["roi"]='-';
            }
        }
        
        $this->assign("kao1",$kao1);
      
        
        
        return $this->fetch();
    }
    public function decisionb($did){
        $de=Db::Table("cg_ti_list")->where(["id"=>$did])->find();
        $apply=Db::table("cg_ti")->where(["id"=>$de["ti_id"]])->find();
        $type_id=$apply["type_id"];
        $jxs=Db::table("cg_jxs")->where(["id"=>$apply["jxs_id"]])->field("zoning,provice,city")->find();
        $de["jxs"]=$apply["jxs_id"];
        $de["fenqu"]=$jxs["zoning"];
        $de["sheng"]=$jxs["provice"];
        $de["shi"]=$jxs["city"];
        $de["sanjifeiyong"]=$de["feetype"];
        $de["erjifeiyong"]=Db::table("cg_feiyongkemu3")->where(["id"=>$de["feetype"]])->value("pid");
        $de["yijifeiyong"]=Db::table("cg_feiyongkemu2")->where(["id"=>$de["erjifeiyong"]])->value("pid");
        if($type_id==1){
                    $de["product"]=$de["f1"];
                    $year=$de["year"];
                    $month=$de["month"];
                    $de["start"]=strtotime("{$year}-{$month}-15");
                    $de["end"]=strtotime("{$year}-{$month}-15");
                }
        $de["starttime"]=$de["start"];$de["endtime"]=$de["end"];
        $zongtianshu=($de["endtime"]-$de["starttime"])/24/3600+1;
        //print_r($de);
        $state=Db::table("cg_ti")->where(["id"=>$de["ti_id"]])->value("status");
        if($state==1){
            //草稿无预算
            $ys=$this->tidys($did);
            //print_r($ys);
            //$ys=[];
        }else{
            //申请有预算
            $ys=Db::table("yusuan")->alias("y")->join("yusuan_detail yd","yd.yid=y.id")->where(["y.type"=>1,"yd.did"=>$did])->select();

        }
        $juece="";
        if($ys){
            $de["channel1"]=$ys[0]["channel1"];
             //有预算
            if($ys[0]["jingxiao"]){
                $jxs=Db::table("cg_jxs")->where(["id"=>$ys[0]["jingxiao"]])->value("title");
                $juece.="经销商:{$jxs}&nbsp;&nbsp;";
            }else if($ys[0]["shi"]){
                $shi=Db::table("cg_market_city")->where(["id"=>$ys[0]["shi"]])->value("title");
                $juece.="城市:{$shi}&nbsp;&nbsp;";
            }else if($ys[0]["sheng"]){
                $sheng=Db::table("cg_market_provice")->where(["id"=>$ys[0]["sheng"]])->value("title");
                $juece.="省份:{$sheng}&nbsp;&nbsp;";
            }else{
                $fenqu=Db::table("cg_market_zoning")->where(["id"=>$de["fenqu"]])->value("title");
                $juece.="分区:{$fenqu}&nbsp;&nbsp;";
            }
            if($ys[0]["sanjifeiyong"]){
                $sanji=Db::table("cg_feiyongkemu3")->where(["id"=>$ys[0]["sanjifeiyong"]])->value("title");
                $juece.="三级费用科目:{$sanji}&nbsp;&nbsp;";
            }else if($ys[0]["erjifeiyong"]){
                $erji=Db::table("cg_feiyongkemu2")->where(["id"=>$ys[0]["erjifeiyong"]])->value("title");
                $juece.="二级费用科目:{$erji}&nbsp;&nbsp;";
            }else{
                $yiji=Db::table("cg_feiyongkemu1")->where(["id"=>$de["yijifeiyong"]])->value("title");
                $juece.="一级费用科目:{$yiji}&nbsp;&nbsp;";
            }
            $channel1=Db::table("cg_channel_types1")->where(["id"=>$de["channel1"]])->value("title");
            $juece.="一级渠道:{$channel1}&nbsp;&nbsp;";
            if($ys[0]["erjikaoheqi"]){
                
                $juece.="考核期:二级考核期";
                $kaohe=2;
            }else{
                $juece.="考核期:一级考核期";
                $kaohe=1;
            }
        }else{
            $channel=Db::table("cg_feiyongkemu3")->where(["id"=>$de["sanjifeiyong"]])->value("channeltypes");
            $channel=unserialize($channel);
            $de["channel1"]=$channel[0];
             //无预算
            $tar=Db::table("target_format")->where(["type_id"=>2])->find();
            if($tar["area_id"]){
                if($tar["area_id"]==8){
                    $jxs=Db::table("cg_jxs")->where(["id"=>$de["jxs"]])->value("title");
                    $juece.="经销商:{$jxs}&nbsp;&nbsp;";
                }else if($tar["area_id"]==22){
                    $shi=Db::table("cg_market_city")->where(["id"=>$de["shi"]])->value("title");
                    $juece.="城市:{$shi}&nbsp;&nbsp;";
                }else if($tar["area_id"]==21){
                    $sheng=Db::table("cg_market_provice")->where(["id"=>$de["sheng"]])->value("title");
                    $juece.="省份:{$sheng}&nbsp;&nbsp;";
                }else{
                    $fenqu=Db::table("cg_market_zoning")->where(["id"=>$de["fenqu"]])->value("title");
                    $juece.="分区:{$fenqu}&nbsp;&nbsp;";
                }
            }else{
                $fenqu=Db::table("cg_market_zoning")->where(["id"=>$de["fenqu"]])->value("title");
                $juece.="分区:{$fenqu}&nbsp;&nbsp;";
            }
            if($tar["feiyong_id"]){
                if($tar["feiyong_id"]==16){
                    $sanji=Db::table("cg_feiyongkemu3")->where(["id"=>$de["sanjifeiyong"]])->value("title");
                    $juece.="三级费用科目:{$sanji}&nbsp;&nbsp;";
                }else if($tar["feiyong_id"]==15){
                    $erji=Db::table("cg_feiyongkemu2")->where(["id"=>$de["erjifeiyong"]])->value("title");
                    $juece.="二级费用科目:{$erji}&nbsp;&nbsp;";
                }else{
                    $yiji=Db::table("cg_feiyongkemu1")->where(["id"=>$de["yijifeiyong"]])->value("title");
                    $juece.="一级费用科目:{$yiji}&nbsp;&nbsp;";
                }
            }else{
                $yiji=Db::table("cg_feiyongkemu1")->where(["id"=>$de["yijifeiyong"]])->value("title");
                $juece.="一级费用科目:{$yiji}&nbsp;&nbsp;";
            }
            $channel1=Db::table("cg_channel_types1")->where(["id"=>$de["channel1"]])->value("title");
            $juece.="一级渠道:{$channel1}&nbsp;&nbsp;";
            if($tar["kaoheqi"]==17){
                $juece.="考核期:一级考核期";
                $kaohe=1;
            }else{
                $juece.="考核期:二级考核期";
                $kaohe=2;
            }
        }
        $this->assign("juece",$juece);
         if($kaohe==1){
            $kaoheqi=Db::table("cg_kaoheqi1")->alias("k1")
            ->join("cg_cainian c","k1.cid=c.id")
            ->where("k1.starttime<={$de["endtime"]} and k1.endtime>={$de["starttime"]}")->order("k1.starttime")
            ->field("k1.title as ktitle,k1.id as kid,c.id as cid,c.title as ctitle,k1.starttime,k1.endtime")->select();
        }else{
            $kaoheqi=Db::table("cg_kaoheqi2")->alias("k2")
            ->join("cg_kaoheqi1 k1","k2.pid=k1.id")
            ->join("cg_cainian c","k1.cid=c.id")
            ->where("k2.starttime<={$de["endtime"]} and k2.endtime>={$de["starttime"]}")->order("k2.starttime")
            ->field("k2.title as ktitle,k2.id as kid,c.id as cid,c.title as ctitle,k2.starttime,k2.endtime")->select();
        }
        $this->assign("kaoheqi",$kaoheqi);
        $ysshuju=[];//预算数据
        $sjshuju=[];//申请数据
        $heji=[];
        $yszong=0;
        $sjzong=0;
        $hezong=0;
        for($i=0;$i<count($kaoheqi);$i++){
            //填充预算数据
            if($ys){
                $flag=0;
                for($j=0;$j<count($ys);$j++){
                    if($kaohe==1){
                        if($ys[$j]["yijikaoheqi"]==$kaoheqi[$i]["kid"]){
                            $zys=Db::table("yusuan")->where(["id"=>$ys[$j]["yid"]])->value("jine");
                            $yyys=Db::table("yusuan_detail")->where(["yid"=>$ys[$j]["yid"],"type"=>2])->sum("jine");
                            if($yyys){
                                $ysshuju[]=$zys-$yyys;
                            }else{
                                $ysshuju[]=$zys;
                            }
                            //$ysshuju[]=Db::table("yusuan")->where(["id"=>$ys[$j]["yid"]])->value("jine");
                            $flag=1;break;
                        }
                    }else{
                        if($ys[$j]["erjikaoheqi"]==$kaoheqi[$i]["kid"]){
                            $zys=Db::table("yusuan")->where(["id"=>$ys[$j]["yid"]])->value("jine");
                            $yyys=Db::table("yusuan_detail")->where(["yid"=>$ys[$j]["yid"],"type"=>2])->sum("jine");
                            if($yyys){
                                $ysshuju[]=$zys-$yyys;
                            }else{
                                $ysshuju[]=$zys;
                            }
                            $flag=1;break;
                        }
                    }
                }
                if(!$flag){
                    $ysshuju[]=0;
                }
            }else{
                $ysshuju[]=0;
            }
            $yszong+=$ysshuju[$i];
            //填充申请数据
            $st=$de["starttime"]>=$kaoheqi[$i]["starttime"]?$de["starttime"]:$kaoheqi[$i]["starttime"];
            $ed=$de["endtime"]<=$kaoheqi[$i]["endtime"]?$de["endtime"]:$kaoheqi[$i]["endtime"];
            $ts=($ed-$st)/3600/24+1;
           // echo $ts."|"."$zongtianshu"."|";
            $sjshuju[]=$de["pizhunhanshui"]/$zongtianshu*$ts;
            $sjzong+=$sjshuju[$i];
            $heji[]=$ysshuju[$i]-$sjshuju[$i];
            $hezong+=$heji[$i];
        }
        $this->assign("hezong",$hezong);
        $this->assign("sjzong",$sjzong);
        $this->assign("yszong",$yszong);
        $this->assign("heji",$heji);
        $this->assign("sjshuju",$sjshuju);
        $this->assign("ysshuju",$ysshuju);
        return $this->fetch();
    }
    public function tidys($did){
        $applyde=Db::table("cg_ti_list")->where(["id"=>$did])->select();
        $aid=$applyde[0]["ti_id"];
        $apply=Db::table("cg_ti")->where(["id"=>$aid])->find();
        $type_id=$apply["type_id"];
        $aptar=Db::table("target_format")->where(["type_id"=>2])->find();
        $jxs=Db::table("cg_jxs")->where(["id"=>$apply["jxs_id"]])->field("zoning,provice,city")->find();
        if($aptar){
            $yusuan=[];

            for($i=0;$i<count($applyde);$i++){
               // echo $i;
               if($type_id==1){
                    $applyde[$i]["product"]=$applyde[$i]["f1"];
                    $year=$applyde[$i]["year"];
                    $month=$applyde[$i]["month"];
                    $applyde[$i]["start"]=strtotime("{$year}-{$month}-15");
                    $applyde[$i]["end"]=strtotime("{$year}-{$month}-15");
                }
                if($applyde[$i]["product"]){
                    $applyde[$i]["sku"]=Db::table("pro_products")->where(["id"=>$applyde[$i]["product"]])->value("sku_id");
                }
                if($applyde[$i]["sku"]){
                    $applyde[$i]["series"]=Db::table("pro_sku")->where(["id"=>$applyde[$i]["sku"]])->value("series_id");
                }
                if($applyde[$i]["series"]){
                    $applyde[$i]["category"]=Db::table("pro_series")->where(["id"=>$applyde[$i]["series"]])->value("category_id");
                }
                if($applyde[$i]["category"]){
                    $applyde[$i]["brand"]=Db::table("pro_category")->where(["id"=>$applyde[$i]["category"]])->value("brand_id");
                }
                $applyde[$i]["jxs"]=$apply["jxs_id"];
                $applyde[$i]["fenqu"]=$jxs["zoning"];
                $applyde[$i]["sheng"]=$jxs["provice"];
                $applyde[$i]["shi"]=$jxs["city"];
                $where="type=1 ";
                $sanjifeiyong=$applyde[$i]["feetype"];
                $erjifeiyong=Db::table("cg_feiyongkemu3")->where(["id"=>$sanjifeiyong])->value("pid");
                $yijifeiyong=Db::table("cg_feiyongkemu2")->where(["id"=>$erjifeiyong])->value("pid");
                $channel=Db::table("cg_feiyongkemu3")->where(["id"=>$sanjifeiyong])->value("channeltypes");
                $channel=unserialize($channel);
                $channelstr=implode(',',$channel);
                //print_r($channel);exit;
                $start=$applyde[$i]["start"];
                $end=$applyde[$i]["end"];
                $tianshu=($end-$start)/24/3600+1;
                if($aptar["product_id"]){
                    if($aptar["product_id"]==11){
                        $where.="and pinpai={$applyde[$i]["brand"]} and pinlei is null and xilie is null ";
                    }else if($aptar["product_id"]==12){
                        if($applyde[$i]["category"]){
                            $where.="and pinpai={$applyde[$i]["brand"]} and pinlei={$applyde[$i]["category"]} and xilie is null ";
                        }else{
                            $where.="and pinpai=-1 ";
                        }
                    }else if($aptar["product_id"]==13){
                        if($applyde[$i]["series"]){
                            $where.="and pinpai={$applyde[$i]["brand"]} and pinlei={$applyde[$i]["category"]} and xilie={$applyde[$i]["series"]} ";
                        }else{
                            $where.="and pinpai=-1 ";
                        }
                    }
                }else{
                    $where.="and pinpai is null and pinlei is null and xilie is null ";
                }  
                if($aptar["area_id"]||$aptar["client_id"]){
                    if($aptar["area_id"]==20){
                        $where.="and fenqu={$applyde[$i]["fenqu"]} and sheng is null and shi is null and jingxiao is null ";
                    }else if($aptar["area_id"]==21){
                        if($applyde[$i]["sheng"]){
                            $where.="and fenqu={$applyde[$i]["fenqu"]} and sheng={$applyde[$i]["sheng"]} and shi is null and jingxiao is null ";
                        }else{
                            $where.="and fenqu=-1 ";
                        }
                    }else if($aptar["area_id"]==22){
                        if($applyde[$i]["shi"]){
                            $where.="and fenqu={$applyde[$i]["fenqu"]} and sheng={$applyde[$i]["sheng"]} and shi={$applyde[$i]["shi"]} and jingxiao is null ";
                        }else{
                            $where.="and fenqu=-1 ";
                        }
                    }else if($aptar["client_id"]==8){
                        if($applyde[$i]["jxs"]){
                            $where.="and fenqu={$applyde[$i]["fenqu"]} and sheng={$applyde[$i]["sheng"]} and shi={$applyde[$i]["shi"]} and jingxiao={$applyde[$i]["jxs"]} ";
                        }else{
                            $where.="and fenqu=-1 ";
                        }
                    }
                }else{
                    $where.="and fenqu is null and sheng is null and shi is null and jingxiao is null ";
                }
                if($aptar["feiyong_id"]){
                    if($aptar["feiyong_id"]==16){
                        $where.="and sanjifeiyong=$sanjifeiyong ";
                    }else if($aptar["feiyong_id"]==15){
                        $where.="and erjifeiyong=$erjifeiyong and sanjifeiyong is null ";
                    }else{
                        $where.="and yijifeiyong=$yijifeiyong and erjifeiyong is null and sanjifeiyong is null ";
                    }
                }else{
                    $where.="and yijifeiyong is null and erjifeiyong is null and sanjifeiyong is null ";
                }
               /* 
                if($aptar["channel_id"]){
                    $where.="and channel1 in ($channelstr) ";
                }*/
               //echo $where;
               if($aptar["kaoheqi"]==18){
                    //二级考核期
                    $kaohes=Db::table("cg_kaoheqi2")->alias("k2")->join("cg_kaoheqi1 k1","k2.pid=k1.id")->where("k2.endtime>=$start and k2.starttime<=$end")->field("k2.id,k2.title,k2.pid,k1.cid,k2.starttime,k2.endtime")->select();
                    $hask=[];
                    for($j=0;$j<count($kaohes);$j++){
                        $hask[]=$kaohes[$j]["id"];
                    }
                    $kstr=implode(',',$hask);
                     $maxc=0;$maxcys=0;
                    for($j=0;$j<count($channel);$j++){
                        $tmpwhere=$where."and channel1={$channel[$j]} and erjikaoheqi in ($kstr) ";
                       // echo $tmpwhere."<br/>";
                        $tmpys=Db::table("yusuan")->where($tmpwhere)->sum("jine");
                        if($tmpys){
                            if($tmpys>$maxc){
                                $maxc=$channel[$j];
                                $maxcys=$tmpys;
                            }
                        }else{
                            $tmpys=0;
                        }
                    }
                     if($maxc){
                        //echo $maxc;
                        $where.="and channel1=$maxc ";
                        for($j=0;$j<count($kaohes);$j++){
                            $jisuanstart=$kaohes[$j]["starttime"]>=$start?$kaohes[$j]["starttime"]:$start;
                            $jisuanend=$kaohes[$j]["endtime"]<=$end?$kaohes[$j]["endtime"]:$end;
                            $ts=($jisuanend-$jisuanstart)/24/3600+1;
                            //echo "-".$ts;
                            $fullwhere=$where."and cainian={$kaohes[$j]["cid"]} and yijikaoheqi={$kaohes[$j]["pid"]} and erjikaoheqi={$kaohes[$j]["id"]}";
                            //echo $fullwhere;exit;
                            $yusuanpipei=Db::table("yusuan")->where($fullwhere)->find();
                            if($yusuanpipei){
                                $yusuanpipei["yid"]=$yusuanpipei["id"];$yusuan[]=$yusuanpipei;
                            }
                        }
                    }else{
                       // break;
                    }
                }else{
                    //一级考核期
                    $kaohes=Db::table("cg_kaoheqi1")->where("endtime>=$start and starttime<=$end")->field("id,title,cid,starttime,endtime")->select();
                    $hask=[];
                    for($j=0;$j<count($kaohes);$j++){
                        $hask[]=$kaohes[$j]["id"];
                    }
                    $kstr=implode(',',$hask);
                    $maxc=0;$maxcys=0;
                    for($j=0;$j<count($channel);$j++){
                        $tmpwhere=$where."and channel1={$channel[$j]} and yijikaoheqi in ($kstr) ";
                        //echo $tmpwhere."<br/>";
                        $tmpys=Db::table("yusuan")->where($tmpwhere)->sum("jine");
                        if($tmpys){
                            if($tmpys>$maxc){
                                $maxc=$channel[$j];
                                $maxcys=$tmpys;
                            }
                        }else{
                            $tmpys=0;
                        }
                        //echo $tmpys."<br>";
                    }
                    if($maxc){
                        //echo $maxc;
                        $where.="and channel1=$maxc ";
                        for($j=0;$j<count($kaohes);$j++){
                            $jisuanstart=$kaohes[$j]["starttime"]>=$start?$kaohes[$j]["starttime"]:$start;
                            $jisuanend=$kaohes[$j]["endtime"]<=$end?$kaohes[$j]["endtime"]:$end;
                            $ts=($jisuanend-$jisuanstart)/24/3600+1;
                            //echo "-".$ts;
                            $fullwhere=$where."and cainian={$kaohes[$j]["cid"]} and  yijikaoheqi={$kaohes[$j]["id"]} and erjikaoheqi is null";
                            //echo $fullwhere;exit;
                            $yusuanpipei=Db::table("yusuan")->where($fullwhere)->find();
                            if($yusuanpipei){
                                $yusuanpipei["yid"]=$yusuanpipei["id"];$yusuan[]=$yusuanpipei;
                            }
                        }
                    }else{
                        //break;
                    }
                }
            }
            //print_r($yusuan);exit;
            return $yusuan;
        }else{
            return [];//未设定格式
        }
    }
    //ti预算匹配
    public function tiys($aid){
        $apply=Db::table("cg_ti")->where(["id"=>$aid])->find();
        $type_id=$apply["type_id"];
        $applyde=Db::table("cg_ti_list")->where(["ti_id"=>$aid])->select();
        $aptar=Db::table("target_format")->where(["type_id"=>2])->find();
        $jxs=Db::table("cg_jxs")->where(["id"=>$apply["jxs_id"]])->field("zoning,provice,city")->find();

        if($aptar){
            $yusuan=[];
           //echo count($applyde);exit;
            for($i=0;$i<count($applyde);$i++){
                if($type_id==1){
                    $applyde[$i]["product"]=$applyde[$i]["f1"];
                    $year=$applyde[$i]["year"];
                    $month=$applyde[$i]["month"];
                    $applyde[$i]["start"]=strtotime("{$year}-{$month}-15");
                    $applyde[$i]["end"]=strtotime("{$year}-{$month}-15");
                }
               // echo $i;exit;
                if($applyde[$i]["product"]){
                    $applyde[$i]["sku"]=Db::table("pro_products")->where(["id"=>$applyde[$i]["product"]])->value("sku_id");
                }
                if($applyde[$i]["sku"]){
                    $applyde[$i]["series"]=Db::table("pro_sku")->where(["id"=>$applyde[$i]["sku"]])->value("series_id");
                }
                if($applyde[$i]["series"]){
                    $applyde[$i]["category"]=Db::table("pro_series")->where(["id"=>$applyde[$i]["series"]])->value("category_id");
                }
                if($applyde[$i]["category"]){
                    $applyde[$i]["brand"]=Db::table("pro_category")->where(["id"=>$applyde[$i]["category"]])->value("brand_id");
                }
                $applyde[$i]["jxs"]=$apply["jxs_id"];
                $applyde[$i]["fenqu"]=$jxs["zoning"];
                $applyde[$i]["sheng"]=$jxs["provice"];
                $applyde[$i]["shi"]=$jxs["city"];
                $where="type=1 ";
                $sanjifeiyong=$applyde[$i]["feetype"];
                $erjifeiyong=Db::table("cg_feiyongkemu3")->where(["id"=>$sanjifeiyong])->value("pid");
                $yijifeiyong=Db::table("cg_feiyongkemu2")->where(["id"=>$erjifeiyong])->value("pid");
                $channel=Db::table("cg_feiyongkemu3")->where(["id"=>$sanjifeiyong])->value("channeltypes");
                $channel=unserialize($channel);
                $channelstr=implode(',',$channel);
                //print_r($channel);exit;
                $start=$applyde[$i]["start"];
                $end=$applyde[$i]["end"];
                $tianshu=($end-$start)/24/3600+1;
                if($aptar["product_id"]){
                    if($aptar["product_id"]==11){
                        $where.="and pinpai={$applyde[$i]["brand"]} and pinlei is null and xilie is null ";
                    }else if($aptar["product_id"]==12){
                        if($applyde[$i]["category"]){
                            $where.="and pinpai={$applyde[$i]["brand"]} and pinlei={$applyde[$i]["category"]} and xilie is null ";
                        }else{
                            $where.="and pinpai=-1 ";
                        }
                    }else if($aptar["product_id"]==13){
                        if($applyde[$i]["series"]){
                            $where.="and pinpai={$applyde[$i]["brand"]} and pinlei={$applyde[$i]["category"]} and xilie={$applyde[$i]["series"]} ";
                        }else{
                            $where.="and pinpai=-1 ";
                        }
                    }
                }else{
                    $where.="and pinpai is null and pinlei is null and xilie is null ";
                }  
                if($aptar["area_id"]||$aptar["client_id"]){
                    if($aptar["area_id"]==20){
                        $where.="and fenqu={$applyde[$i]["fenqu"]} and sheng is null and shi is null and jingxiao is null ";
                    }else if($aptar["area_id"]==21){
                        if($applyde[$i]["sheng"]){
                            $where.="and fenqu={$applyde[$i]["fenqu"]} and sheng={$applyde[$i]["sheng"]} and shi is null and jingxiao is null ";
                        }else{
                            $where.="and fenqu=-1 ";
                        }
                    }else if($aptar["area_id"]==22){
                        if($applyde[$i]["shi"]){
                            $where.="and fenqu={$applyde[$i]["fenqu"]} and sheng={$applyde[$i]["sheng"]} and shi={$applyde[$i]["shi"]} and jingxiao is null ";
                        }else{
                            $where.="and fenqu=-1 ";
                        }
                    }else if($aptar["client_id"]==8){
                        if($applyde[$i]["jxs"]){
                            $where.="and fenqu={$applyde[$i]["fenqu"]} and sheng={$applyde[$i]["sheng"]} and shi={$applyde[$i]["shi"]} and jingxiao={$applyde[$i]["jxs"]} ";
                        }else{
                            $where.="and fenqu=-1 ";
                        }
                    }
                }else{
                    $where.="and fenqu is null and sheng is null and shi is null and jingxiao is null ";
                }
                if($aptar["feiyong_id"]){
                    if($aptar["feiyong_id"]==16){
                        $where.="and sanjifeiyong=$sanjifeiyong ";
                    }else if($aptar["feiyong_id"]==15){
                        $where.="and erjifeiyong=$erjifeiyong and sanjifeiyong is null ";
                    }else{
                        $where.="and yijifeiyong=$yijifeiyong and erjifeiyong is null and sanjifeiyong is null ";
                    }
                }else{
                    $where.="and yijifeiyong is null and erjifeiyong is null and sanjifeiyong is null ";
                }
               /* 
                if($aptar["channel_id"]){
                    $where.="and channel1 in ($channelstr) ";
                }*/
               //echo $where;echo "<br/>";exit;
               if($aptar["kaoheqi"]==18){
                ///echo "---";
                    //二级考核期
                    $kaohes=Db::table("cg_kaoheqi2")->alias("k2")->join("cg_kaoheqi1 k1","k2.pid=k1.id")->where("k2.endtime>=$start and k2.starttime<=$end")->field("k2.id,k2.title,k2.pid,k1.cid,k2.starttime,k2.endtime")->select();
                    $hask=[];
                    for($j=0;$j<count($kaohes);$j++){
                        $hask[]=$kaohes[$j]["id"];
                    }
                    $kstr=implode(',',$hask);
                     $maxc=0;$maxcys=0;
                     //print_r($channel);
                    for($j=0;$j<count($channel);$j++){
                        $tmpwhere=$where."and channel1={$channel[$j]} and erjikaoheqi in ($kstr) ";
                       // echo $tmpwhere."<br/>";
                        $tmpys=Db::table("yusuan")->where($tmpwhere)->sum("jine");
                        //echo db("yusuan")->getLastsql()."<br/>";
                        if($tmpys){
                            if($tmpys>$maxc){
                                $maxc=$channel[$j];
                                $maxcys=$tmpys;
                            }
                        }else{
                            $tmpys=0;
                        }
                    }
                     if($maxc){
                        //echo $maxc;
                        $where.="and channel1=$maxc ";
                        for($j=0;$j<count($kaohes);$j++){
                            $jisuanstart=$kaohes[$j]["starttime"]>=$start?$kaohes[$j]["starttime"]:$start;
                            $jisuanend=$kaohes[$j]["endtime"]<=$end?$kaohes[$j]["endtime"]:$end;
                            $ts=($jisuanend-$jisuanstart)/24/3600+1;
                            //echo "-".$ts;
                            $fullwhere=$where."and cainian={$kaohes[$j]["cid"]} and yijikaoheqi={$kaohes[$j]["pid"]} and erjikaoheqi={$kaohes[$j]["id"]}";
                           // echo $fullwhere;echo "<br/>";//exit;
                            $yusuanpipei=Db::table("yusuan")->where($fullwhere)->find();
                            if($yusuanpipei){
                                $hanshui=($applyde[$i]["yjsjtrfy"])/$tianshu*$ts;
                                $tmpy=["yid"=>$yusuanpipei["id"],"did"=>$applyde[$i]["id"],"aid"=>$aid,"jine"=>$hanshui,"type"=>1,"tianshu"=>$ts,"zongtianshu"=>$tianshu];
                                $yusuan[]=$tmpy;
                            }
                        }
                    }else{
                       // break;
                    }
                }else{
                   // echo "---";
                    //一级考核期
                    $kaohes=Db::table("cg_kaoheqi1")->where("endtime>=$start and starttime<=$end")->field("id,title,cid,starttime,endtime")->select();
                    $hask=[];
                    for($j=0;$j<count($kaohes);$j++){
                        $hask[]=$kaohes[$j]["id"];
                    }
                    $kstr=implode(',',$hask);
                    $maxc=0;$maxcys=0;
                    for($j=0;$j<count($channel);$j++){
                        $tmpwhere=$where."and channel1={$channel[$j]} and yijikaoheqi in ($kstr) ";
                        //echo $tmpwhere."<br/>";
                        $tmpys=Db::table("yusuan")->where($tmpwhere)->sum("jine");
                        if($tmpys){
                            if($tmpys>$maxc){
                                $maxc=$channel[$j];
                                $maxcys=$tmpys;
                            }
                        }else{
                            $tmpys=0;
                        }
                        //echo $tmpys."<br>";
                    }
                    if($maxc){
                        //echo $maxc;
                        $where.="and channel1=$maxc ";
                        for($j=0;$j<count($kaohes);$j++){
                            $jisuanstart=$kaohes[$j]["starttime"]>=$start?$kaohes[$j]["starttime"]:$start;
                            $jisuanend=$kaohes[$j]["endtime"]<=$end?$kaohes[$j]["endtime"]:$end;
                            $ts=($jisuanend-$jisuanstart)/24/3600+1;
                            //echo "-".$ts;
                            $fullwhere=$where."and cainian={$kaohes[$j]["cid"]} and  yijikaoheqi={$kaohes[$j]["id"]} and erjikaoheqi is null";
                            //echo $fullwhere;echo "<br/>";//exit;
                            $yusuanpipei=Db::table("yusuan")->where($fullwhere)->find();
                            if($yusuanpipei){
                                $hanshui=($applyde[$i]["yjsjtrfy"])/$tianshu*$ts;
                                $tmpy=["yid"=>$yusuanpipei["id"],"did"=>$applyde[$i]["id"],"aid"=>$aid,"jine"=>$hanshui,"type"=>1,"tianshu"=>$ts,"zongtianshu"=>$tianshu];
                                $yusuan[]=$tmpy;
                            }
                        }
                    }else{
                       // break;
                    }

                } 
              //  echo $i;
            }
            //exit;
            //print_r($yusuan);exit;
            return $yusuan;
        }else{
            return 0;//未设定格式
        }
       
    }
}